package com.hsxc.cdz.monitor.handler;

import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.FSM.CachePile;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 刘力文
 *         处理通道连接状态的类
 */
public class StateInHandler extends ChannelInboundHandlerAdapter {

    private Logger logger = LoggerFactory.getLogger(StateInHandler.class);
    // 判断连接是否空闲，三次空闲关闭通道
    private int state = 0;


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idle = (IdleStateEvent) evt;
            switch (idle.state()) {
                case READER_IDLE:
                    if (state >= 3) {
                        CachePile cachePile = GlobalManager.pileManager.getPile(ctx);
                        if (cachePile != null) {
                            logger.error("正在断开桩连接,桩号" + cachePile.getAddress());
                            cachePile.offLine();
                        }
                        if (ctx != null) {
                            ctx.channel().close();
                        }
                        //state=0;
                       logger.error("一个客户端断开连接");
                    } else {
                        state++;
                    }
                   logger.info("读操作空闲");
                    break;
                case ALL_IDLE:
                    if (state >= 3) {
                        CachePile cachePile = GlobalManager.pileManager.getPile(ctx);
                        if (cachePile != null) {
                            cachePile.offLine();
                        }
                        if (ctx != null) {
                            ctx.channel().close();
                        }
                        //state=0;
                        logger.error("一个客户端断开连接");
                        logger.error("一个客户端断开连接pileAddress" + cachePile.getAddress());
                    } else {
                        state++;
                    }
                    logger.info("连接空闲");
                    break;
                case WRITER_IDLE:
                    if (state >= 3) {
                        CachePile cachePile = GlobalManager.pileManager.getPile(ctx);
                        if (cachePile != null) {
                            cachePile.offLine();
                        }
                        if (ctx != null) {
                            ctx.channel().close();
                        }
                        //state=0;
                        logger.error("一个客户端断开连接");
                        logger.error("一个客户端断开连接pileAddress" + cachePile.getAddress());
                    } else {
                        state++;
                    }
                    logger.info("写操作空闲");
                    break;
                default:
            }
        }
    }

    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        //进行读操作，通道使用
        state = 0;
        ctx.fireChannelRead(msg);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) {
        System.out.println("加入一个连接");
        //CachePile cachePile = GlobalManager.pileManager.getPile(ctx);
        //System.out.println("一个客户端加入连接"+cachePile.getAddress());

    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) {
        System.out.println("移除一个连接:" + ctx.channel().hashCode());
        CachePile cachePile = GlobalManager.pileManager.getPile(ctx);
        if (cachePile != null) {
            cachePile.offLine();
        }
        if (ctx != null) {
            ctx.channel().close();
        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        Channel incoming = ctx.channel();
        if(!incoming.isActive())
            System.out.println("SimpleClient:" + incoming.remoteAddress()
                    + "异常");
        cause.printStackTrace();
        ctx.close();
    }
}
